/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CameraFeedTopComponent.java
 *
 * Created on Jul 26, 2011, 11:00:01 AM
 */
package pt.utl.ist.isr.livetrack.ui;

import java.awt.Image;
import java.util.HashMap;
import java.util.Map;
import org.openide.util.ImageUtilities;
import org.openide.util.lookup.Lookups;
import org.openide.windows.TopComponent;
import pt.utl.ist.isr.livetrack.server.LTServer;
import pt.utl.ist.isr.livetrack.server.interfaces.Camera;

/**
 *
 * @author David Miguel Antunes <davidmiguel [ at ] antunes.net>
 */
@TopComponent.Description(preferredID = "CameraFeedTopComponent",
iconBase = "pt/utl/ist/isr/livetrack/ui/icons/server.jpg",
persistenceType = TopComponent.PERSISTENCE_NEVER)
public class CameraFeedTopComponent extends TopComponent {

    private static final Map<String, CameraFeedTopComponent> instances = new HashMap<String, CameraFeedTopComponent>();
    private Camera camera;
    private CameraNode cameraNode;

    public static void openCameraFeed(LTServer server, Camera camera, CameraNode cameraNode) {
        String serverString = camera.getId() + "@" + server.getHostname() + ":" + server.getPort();
        CameraFeedTopComponent feed;
        if (instances.containsKey(serverString)) {
            feed = instances.get(serverString);
        } else {
            feed = new CameraFeedTopComponent(server, camera, cameraNode);
            instances.put(serverString, feed);
        }
        feed.open();
        feed.requestActive();
    }

    /** Creates new form CameraFeedTopComponent */
    private CameraFeedTopComponent(final LTServer server, final Camera camera, final CameraNode cameraNode) {
        String serverString = camera.getId() + "@" + server.getHostname() + ":" + server.getPort();
        this.camera = camera;
        this.cameraNode = cameraNode;
        setDisplayName(serverString);
        initComponents();
        associateLookup(Lookups.singleton(cameraNode));
        viewPanel.add(cameraNode.getCameraView());
        controllerPanel.add(cameraNode.getCameraViewController());
    }

    @Override
    public int getPersistenceType() {
        return PERSISTENCE_NEVER;
    }

    public Image getIcon(int type) {
        return ImageUtilities.loadImage("pt/utl/ist/isr/livetrack/ui/icons/tv.jpg", true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        controllerPanel = new javax.swing.JPanel();
        viewPanel = new javax.swing.JPanel();

        controllerPanel.setLayout(new javax.swing.BoxLayout(controllerPanel, javax.swing.BoxLayout.LINE_AXIS));

        viewPanel.setLayout(new javax.swing.BoxLayout(viewPanel, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(viewPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE)
                    .addComponent(controllerPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(viewPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(controllerPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel controllerPanel;
    private javax.swing.JPanel viewPanel;
    // End of variables declaration//GEN-END:variables
}
